<?php
declare (strict_types=1);

namespace app\command;

use app\webapi\controller\ApiRedis;
use app\webapi\controller\APiRedisServer;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;

class syncRushTask extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('syncrushtask')
            ->setDescription('同步抢购商品任务');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $output->writeln('同步抢购商品任务');

        $data = ApiRedis::updateGet('rush_schedule', function () {
            $sql = "SELECT * FROM sy_rush_schedule ORDER BY starttime ASC ";
            $sql_result = Db::query($sql, []);
            return $sql_result;
        });
        $curTime = time();
        $Ymd= date('Y-m-d',time());
        foreach ($data as $v) {
            $startArr = explode(':', $v['starttime']);
            $start = strtotime($Ymd . ' ' . $startArr[0] . ':' . $startArr[1] . ':' . $startArr[2]);
            if ($start > $curTime && ($start - $curTime) <= 1730) {
                $sql = "SELECT a.*,a.sid,b.nickname belong_nickname,b.avatar belong_avatar  
                            FROM sy_goods_rush a 
                            LEFT JOIN sy_user b ON b.id=a.belong 
                            WHERE a.sid = ? AND a.state IN (0,1,2,3,4,5)";
                $goods_data = Db::query($sql, [$v['id']]);
                // 清除抢购列表键
                $itemKeys = APiRedisServer::selected(0)->keys("rush_goods_" . $v['id']);

                if (count($itemKeys) > 0){
                    APiRedisServer::selected(0)->del($itemKeys);
                }
                // 添加商品至抢购列表
                foreach ($goods_data as $gv){
                    APiRedisServer::selected(0)->hset("rush_goods_" . $v['id'],$gv['id'],json_encode($gv,JSON_UNESCAPED_UNICODE));
                }
                // 添加计数器
                foreach ($goods_data as $gdv){
                    $count = 0;
                    if($gdv['state'] == 1){
                        $count = 1;
                    }
                    $map[ $gdv['id'] ] = $count;
                    APiRedisServer::selected(0)->hdel("counter_rush_goods_".$v['id'],$gdv['id']);
                    APiRedisServer::selected(0)->hmset("counter_rush_goods_".$v['id'],$map);
                }
                $output->writeln("更新抢购场次: {$v['name']} Id: {$v['id']} SID: {$v['id']} time::".date('Y-m-d H:i:s',time()));
            }
        }
         /**  更新用户发布的商品 */
        $allusers = Db::query("SELECT id FROM sy_user WHERE state=1",[]);
        $uids = array_column($allusers,'id');
        foreach ($uids as $uv){
            $belongUsers = Db::query("SELECT id FROM sy_goods_rush WHERE belong=? AND state IN (0,1,2,3)", [$uv]);
            APiRedisServer::selected(2)->hdel("user_blong", $uv);
            if(count($belongUsers) > 0){
                $gidList = array_column($belongUsers,'id');
                $map1[ $uv ] = json_encode($gidList,JSON_UNESCAPED_UNICODE);
                APiRedisServer::selected(2)->hmset("user_blong",$map1);
            }
        }


    }

}
